# statis_http_detail 表数据从金华迁移到石家庄，迁移1年半数据


import datetime

import pymysql
import sys

if __name__ == "__main__":
    conn = pymysql.connect(
        host="10.103.3.12",
        port=9030,
        user="portal",
        password="portal@Baishancloud#1qaz",
        database="cache_log"
    )
    cursor = conn.cursor()
    sql_domain = 'select distinct id from dim_domain;'
    domains = []
    cursor.execute(sql_domain)
    results = cursor.fetchall()
    for row in results:
        domains.append(row[0])

    # begin = "2021-07-25 00:00:00"
    # end = "2022-01-18 19:05:00"
    begin, end, log_file_path = sys.argv[1], sys.argv[2], sys.argv[3]
    log_file = open(log_file_path, mode='a+')
    fmt = "%Y-%m-%d %H:%M:%S"
    dt_begin = datetime.datetime.strptime(begin, fmt)
    dt_end = datetime.datetime.strptime(end, fmt)
    while dt_begin < dt_end:
        time_at_end = dt_end
        time_at_start = dt_end - datetime.timedelta(hours=6)
        sql = 'insert into statis_http_detail (domain, time_at_1min, is_parent, http_code, is_hit, is_abroad, is_https, is_ipv6, vendor, time_at_5min, time_at_1hour, time_at_1day, request, traffic, pv) select domain, time_at_1min, is_parent, http_code, is_hit, is_abroad, is_https, is_ipv6, vendor, time_at_5min, time_at_1hour, time_at_1day, request, traffic, pv from statis_http_detail_jh where time_at_1min >= "%s" and time_at_1min < "%s"' % (
            time_at_start, time_at_end)
        log_file.write(
            '\n\n\n\nstatis_http_detail\t' + str(datetime.datetime.now()) + '\n' + str(time_at_start) + "\t" + str(time_at_end) + '\n')
        count = 0
        threshold = 1000
        sql1 = sql + ' and domain in ('
        for domain in domains:
            sql1 += str(domain) + ','
            count += 1
            if count % threshold == 0:
                sql1 = sql1.rstrip(',') + ");"
                log_file.write(sql1 + '\n')
                log_file.flush()
                cursor.execute(sql1)
                sql1 = sql + ' and domain in ('
        sql1 = sql1.rstrip(',') + ");"
        log_file.write(sql1 + '\n')
        cursor.execute(sql1)
        dt_end -= datetime.timedelta(hours=6)
        log_file.flush()
    log_file.close()
